﻿using DFGK.IService;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.入库管理
{
    /// <summary>
    /// 入库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class ReceivingOrderDetailController : BaseController
    {
        private readonly IReceivingOrderDetailService receivingOrderDetailService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="receivingOrderDetailService"></param>
        public ReceivingOrderDetailController(IWebHostEnvironment environment, IReceivingOrderDetailService receivingOrderDetailService) : base(environment)
        {
            this.receivingOrderDetailService = receivingOrderDetailService;
        }

        /// <summary>
        /// PC收货入库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BuildCreateStackingStorage(ReceivingBillOrderDto m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            string message = "";
            m.NowUserId = base.BaseGetNowUserId();
            m.NowUserName = base.BaseGetNowUserName();
            bool IsOK = receivingOrderDetailService.AddReceivingOrderDetail(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "保存成功！分配的仓位为：" + message, "200"));
            }
            return Ok(BaseResult.Result(false, "保存失败" + message, "500"));
        }

        /// <summary>
        /// 获取入库单列表
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="orderSn"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetReceivingOrderDetailList(DateTime? startTime=null, DateTime? endTime = null, string? orderSn="", int status=8, int pageIndex=1, int pageSize = 8)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            int totalCount;
            var list = receivingOrderDetailService.GetReceivingOrderDetailList(startTime, endTime, orderSn, status, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
